package me.zhengjie.modules.weixin.utils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具
 * 
 * @author lisong
 */
public class DateUtil {

	/** 日期格式 **/
	public static final String DATE_PATTERN_HH_MM_SS = "HH:mm:ss";
	public static final String DATE_PATTERN_HH_MM = "HH:mm";
	public static final String DATE_PATTERN_YYYY_MM_DD = "yyyy-MM-dd";
	public static final String DATE_PATTERN_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
	public static final String DATE_PATTERN_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_PATTERN_YYYY_MM_DD_HH_MM_SS_1 = "yyyy/MM/dd HH:mm:ss";
	
	public static String date2String(Date date) {
		return date2String(date, DATE_PATTERN_YYYY_MM_DD_HH_MM_SS);
	}

	public static String date2String(Date date, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}
	
	public static Date string2Date(String dateString) {
		return string2Date(dateString, DATE_PATTERN_YYYY_MM_DD_HH_MM_SS);
	}

	public static Date string2Date(String dateString, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		ParsePosition pos = new ParsePosition(0);
		return format.parse(dateString, pos);
	}
	
	public static Date getMonthStartDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	public static Date getMonthEndDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, -1);
		return cal.getTime();
	}
	
	public static Date getDayStartDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	public static Date getDayEndDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, -1);
		return cal.getTime();
	}
	
	public static Date addMonth(Date date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, amount);
		return cal.getTime();
	}

	public static Date addDay(Date date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, amount);
		return cal.getTime();
	}

	public static Date addHour(Date date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR_OF_DAY, amount);
		return cal.getTime();
	}

	public static Date addMinute(Date date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, amount);
		return cal.getTime();
	}

	/**
	 * 校验日起是否合法
	 * @param s 日起字符串
	 * @param pattern 日起格式
	 * @return true：合法，false：不合法
	 */
	public static boolean isValidDate(String s,String pattern)
	{
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		format.setLenient(false);
		try
		{
			format.parse(s);
			return true;
		}
		catch (Exception e)
		{
			return false;
		}
	}

	/**
	 * 判断生日是否成年 格式固定为2015-05-10
	 * @param s
	 * @return
	 */
	public static boolean isGrownUp(String s) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		simpleDateFormat.setLenient(false);
		Date update;
		try {
			String tempDate = simpleDateFormat.format(simpleDateFormat.parse(s)).replaceAll("-","");
			int year = Integer.parseInt(s.substring(0, 4));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			sdf.setLenient(false);
			update = sdf.parse(String.valueOf(year + 18) + tempDate.substring(4, 8));
		} catch (ParseException e) {
			return false;
		}
		Date today = new Date();
		return today.after(update);
	}

	public static Date geLastWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday(date));
		cal.add(Calendar.DATE, -7);
		return cal.getTime();
	}

	public static Date getThisWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 获得当前日期是一个星期的第几天
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		// 获得当前日期是一个星期的第几天
		int day = cal.get(Calendar.DAY_OF_WEEK);
		// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
		return cal.getTime();
	}

	public static Date getNextWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday(date));
		cal.add(Calendar.DATE, 7);
		return cal.getTime();
	}
	/**
	 * 获取两个日期之间的分钟数
	 * @param before
	 * @param after
	 * @return
	 */
	public static long getMinutesOf2Date(Date before, Date after){
		return getSecondsOf2Date(before, after)/60;
	}

	/**
	 * 获取两个日期之间的秒数
	 * @param before
	 * @param after
	 * @return
	 */
	public static long getSecondsOf2Date(Date before, Date after){
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (afterTime - beforeTime) / 1000;
	}

	public static Date getCurrentDate() {
		return string2Date(date2String(new Date()), DATE_PATTERN_YYYY_MM_DD);
	}
	public static void main(String[] args) throws Exception {
		System.out.println(isValidDate("2015-1-2",DATE_PATTERN_YYYY_MM_DD));
		System.out.println(isGrownUp("1999-08-16"));
		System.out.println(getCurrentDate());
	}
}
